package com.qeegoo.b2bautozityp.moudle.order.viewmodel;

//┏┓　　　┏┓ 
//┏┛┻━━━┛┻┓ 
//┃　　　　　　　┃ 　 
//┃　　　━　　　┃ 
//┃　┳┛　┗┳　┃ 
//┃　　　　　　　┃ 
//┃　　　┻　　　┃ 
//┃　　　　　　　┃ 
//┗━┓　　　┏━┛ 
//┃　　　┃  神兽保佑　　　　　　　　 
//┃　　　┃  代码无BUG！ 
//┃　　　┗━━━┓ 
//┃　　　　　　　┣┓ 
//┃　　　　　　　┏┛ 
//┗┓┓┏━┳┓┏┛ 
// ┃┫┫　┃┫┫ 
// ┗┻┛　┗┻┛

import android.databinding.ObservableField;
import android.view.View;

import com.kelin.mvvmlight.messenger.Messenger;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.constant.Constants;
import com.qeegoo.b2bautozityp.moudle.order.model.OrderData;
import com.qeegoo.b2bautozityp.moudle.order.model.OrderResultBean;
import com.qeegoo.b2bautozityp.utils.AdapterInterface;
import com.qeegoo.b2bautozityp.widget.OrderDeliveryDialog;
import com.qeegoo.b2bautozityp.widget.OrderDeliveryDialog.OnDialogFinishListener;

import base.lib.util.ActivityManager;
import base.lib.util.ToastUtils;

/**
 * Created by lk on 2017/9/14.
 */

public class OrderListViewModel {
    private OrderDeliveryDialog orderDeliveryDialog;
    private final RequestApi requestApi;
    private int mType;
    public ObservableField<Integer> emptyVisible = new ObservableField<>(View.GONE);
    private AdapterInterface<OrderResultBean> adapterInterface;
    private String tag;

    public int getmType() {
        return mType;
    }

    public void setmType(int mType) {
        this.mType = mType;
    }

    public OrderListViewModel(RequestApi api ) {
        this.requestApi = api;

        // 接受 来自 orderAdapter的 对应的view的点击
        Messenger.getDefault().register(this, mType, OrderData.class, data -> {

            orderDelivery(data.type, data.code);
        });
    }

    //订单
    public void getDatas(String pageNo, String tag) {
        this.tag = tag;
        requestApi.orderLists(HttpParams.paramOrderLists(pageNo, 10 + "", tag, ""))
                .map(new RetrofitService.HttpResultFunc<>())
                .compose(RetrofitService.applySchedulers())
                .subscribe(orderResultBean -> {
                    adapterInterface.callBackData(orderResultBean);

                }, throwable -> {
                    ToastUtils.showToast(throwable.getMessage());
                });
    }

    //退单
    public void getDatas1(String pageNo, String tag) {
        this.tag = tag;
        requestApi.orderReturnLists(HttpParams.paramOrderLists(pageNo, 10 + "", tag, ""))
                .map(new RetrofitService.HttpResultFunc<>())
                .compose(RetrofitService.applySchedulers())
                .subscribe(orderResultBean -> {
                    adapterInterface.callBackData(orderResultBean);

                }, throwable -> {
                    ToastUtils.showToast(throwable.getMessage());
                });
    }

    public void setAdapterInterface(AdapterInterface<OrderResultBean> orderResultBeanAdapterInterface) {
        this.adapterInterface = orderResultBeanAdapterInterface;
    }

    public void orderDelivery(int type, String code) {
        if (type == Constants.AdapterType_0) {
            requestApi.orderDelivery(HttpParams.paramViewByCode(code))
                    .map(new RetrofitService.HttpResultFunc())
                    .compose(RetrofitService.applySchedulers())
                    .subscribe(new ProgressSubscriber() {
                        @Override
                        public void onNext(Object o) {
                            orderDeliveryDialog = new OrderDeliveryDialog(ActivityManager.getActivity(), new OnDialogFinishListener() {
                                @Override
                                public void onFinish() {
                                    if (orderDeliveryDialog != null) {
                                        orderDeliveryDialog.dismiss();
                                        getDatas("1", tag);
                                    }
                                }

                                @Override
                                public void onCancel() {

                                }
                            });
                            orderDeliveryDialog.show();
                        }
                    });
        } else {
            requestApi.confirmReceipt(HttpParams.paramViewByCode(code))
                    .map(new RetrofitService.HttpResultFunc())
                    .compose(RetrofitService.applySchedulers())
                    .subscribe(new ProgressSubscriber() {
                        @Override
                        public void onNext(Object o) {
                            ToastUtils.showToast("确认成功！");
                            getDatas1("1",tag);
                        }
                    });
        }

    }
}
